import tkinter as tk
from tkinter import ttk, messagebox
import random

class RiddleGame:
    def __init__(self, master):
        self.master = master
        self.master.title("猜谜语游戏")
        self.master.geometry("500x400")
        self.master.resizable(False, False)
        
        # 确保中文显示正常
        self.master.option_add("*Font", "SimHei 10")
        
        # 谜语题库
        self.riddles = [
            {"question": "什么东西越洗越脏，不洗有人吃，洗了没人吃？", "answer": "水"},
            {"question": "什么东西越生气，它就越大？", "answer": "脾气"},
            {"question": "什么东西没吃的时候是绿的，吃的时候是红的，吐出来的是黑的？", "answer": "西瓜"},
            {"question": "什么东西越用越有钱？", "answer": "存钱罐"},
            {"question": "什么东西越走越轻？", "answer": "气球"},
            {"question": "什么东西越冷越爱出来？", "answer": "霜"},
            {"question": "什么东西越热越爱出来？", "answer": "汗"},
            {"question": "什么东西越老越年轻？", "answer": "照片"},
            {"question": "什么东西越跑越慢？", "answer": "电池"},
            {"question": "什么东西越擦越小？", "answer": "橡皮"}
        ]
        
        # 游戏设置
        self.current_riddle = None
        self.score = 0
        self.attempts = 0
        
        # 创建UI
        self.create_ui()
        
        # 开始游戏
        self.next_riddle()
    
    def create_ui(self):
        """创建用户界面"""
        # 创建顶部标题
        title_label = tk.Label(
            self.master,
            text="猜谜语游戏",
            font=("SimHei", 16, "bold")
        )
        title_label.pack(pady=10)
        
        # 创建分数显示
        self.score_var = tk.StringVar(value=f"得分: {self.score} | 尝试次数: {self.attempts}")
        self.score_label = tk.Label(
            self.master,
            textvariable=self.score_var,
            font=("SimHei", 12)
        )
        self.score_label.pack(pady=5)
        
        # 创建谜语显示
        self.riddle_frame = tk.Frame(self.master)
        self.riddle_frame.pack(pady=20)
        
        self.riddle_label = tk.Label(
            self.riddle_frame,
            text="",
            font=("SimHei", 14),
            wraplength=400
        )
        self.riddle_label.pack()
        
        # 创建答案输入框
        self.answer_frame = tk.Frame(self.master)
        self.answer_frame.pack(pady=10)
        
        self.answer_var = tk.StringVar()
        self.answer_entry = ttk.Entry(
            self.answer_frame,
            textvariable=self.answer_var,
            width=30
        )
        self.answer_entry.pack(side=tk.LEFT, padx=5)
        
        self.submit_btn = ttk.Button(
            self.answer_frame,
            text="提交答案",
            command=self.check_answer
        )
        self.submit_btn.pack(side=tk.LEFT, padx=5)
        
        # 创建提示标签
        self.hint_var = tk.StringVar(value="")
        self.hint_label = tk.Label(
            self.master,
            textvariable=self.hint_var,
            font=("SimHei", 12),
            fg="red"
        )
        self.hint_label.pack(pady=10)
        
        # 创建操作按钮
        self.control_frame = tk.Frame(self.master)
        self.control_frame.pack(pady=20)
        
        self.skip_btn = ttk.Button(
            self.control_frame,
            text="跳过",
            command=self.next_riddle
        )
        self.skip_btn.pack(side=tk.LEFT, padx=10)
        
        self.quit_btn = ttk.Button(
            self.control_frame,
            text="退出",
            command=self.quit_game
        )
        self.quit_btn.pack(side=tk.LEFT, padx=10)
    
    def next_riddle(self):
        """获取下一个谜语"""
        # 如果题库为空，重新加载
        if not self.riddles:
            self.riddles = [
                {"question": "什么东西越洗越脏，不洗有人吃，洗了没人吃？", "answer": "水"},
                {"question": "什么东西越生气，它就越大？", "answer": "脾气"},
                {"question": "什么东西没吃的时候是绿的，吃的时候是红的，吐出来的是黑的？", "answer": "西瓜"},
                {"question": "什么东西越用越有钱？", "answer": "存钱罐"},
                {"question": "什么东西越走越轻？", "answer": "气球"},
                {"question": "什么东西越冷越爱出来？", "answer": "霜"},
                {"question": "什么东西越热越爱出来？", "answer": "汗"},
                {"question": "什么东西越老越年轻？", "answer": "照片"},
                {"question": "什么东西越跑越慢？", "answer": "电池"},
                {"question": "什么东西越擦越小？", "answer": "橡皮"}
            ]
        
        # 随机选择一个谜语
        self.current_riddle = random.choice(self.riddles)
        self.riddles.remove(self.current_riddle)
        
        # 更新UI
        self.riddle_label.config(text=self.current_riddle["question"])
        self.hint_var.set("")
        self.answer_var.set("")
        
        # 更新尝试次数
        self.attempts += 1
        self.score_var.set(f"得分: {self.score} | 尝试次数: {self.attempts}")
    
    def check_answer(self):
        """检查答案"""
        user_answer = self.answer_var.get().strip()
        
        if not user_answer:
            self.hint_var.set("请输入答案！")
            return
        
        if user_answer == self.current_riddle["answer"]:
            # 答对了
            self.score += 10
            self.hint_var.set(f"恭喜你，答对了！答案是：{self.current_riddle['answer']}")
            self.score_var.set(f"得分: {self.score} | 尝试次数: {self.attempts}")
            
            # 3秒后自动下一个谜语
            self.master.after(3000, self.next_riddle)
        else:
            # 答错了
            self.hint_var.set(f"答错了，再试一次！")
            self.attempts += 1
            self.score_var.set(f"得分: {self.score} | 尝试次数: {self.attempts}")
    
    def quit_game(self):
        """退出游戏"""
        if messagebox.askyesno("确认", "确定要退出游戏吗？"):
            result = f"游戏结束！\n你的总得分是: {self.score}\n尝试次数: {self.attempts}"
            messagebox.showinfo("游戏结果", result)
            self.master.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    game = RiddleGame(root)
    root.mainloop()